home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / c_news / 15 / cnews015.doc next >
Text File  |  1989-07-05  |  39KB  |  1,224 lines

  1.  
  2.  
  3.       Issue 15                  C News                              1
  4.  
  5.  
  6.       *-------------------------------------------------------------*
  7.       |   C NEWS - International C Electronic Newsletter/Journal    |
  8.       |          "Dedicated to the Art of C Programming"            |
  9.       |                                                             |
  10.       |                     Founded 12/27/87                        |
  11.       *-------------------------------------------------------------*
  12.  
  13.  
  14.                              Table of Contents
  15.  
  16.  
  17.       Heap: A Message from the Editor ............................. 2
  18.  
  19.       Linked Lists: An Introduction by A. Lander .................. 4
  20.  
  21.       Beginners Corner: By W.Dernoncourt ..........................12
  22.  
  23.       Beginners Corner Pt II: By W.Dernoncourt ....................15
  24.  
  25.       Attachments .................................................20
  26.  
  27.  
  28.  
  29.       "C  News"  is  an  Electronic  Journal published by the C BBS in
  30.       Burke,  VA  on  a monthly basis. The subject for C News is the C
  31.       programming language, as well as any derivatives like C++.  
  32.  
  33.       All  readers  are  encouraged  to  submit  articles, reviews, or
  34.       comments  for submission.  C News is freely distributed, but can
  35.       not  be  sold  for a profit, or cannot have a charge assessed to
  36.       cover  distribution costs.  All articles, and reviews become the
  37.       property   of   C   News   and   cannot  be  included  in  other
  38.       publications   without   written  permission  from  the  C  News
  39.       Editorial  Staff.  To do so is in direct violation of the C News
  40.       License  agreement.   Copies  of  which are available from the C
  41.       BBS.    This  publication  is  Copyrighted  under  U.S Copyright
  42.       Law.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.       Issue 15                  C News                               2
  60.  
  61.  
  62.       ================================================================
  63.       THE HEAP: MESSAGES FROM THE EDITOR
  64.       ================================================================
  65.  
  66.       HELLO AGAIN!!!
  67.  
  68.       After  quite  a  few  months,  "C News" finally hits the digital
  69.       data  streams again.  Here at the C BBS we have suffered through
  70.       hardrive  crashes, turmoil, and general burnout.  But I am happy
  71.       to  report  that we are back, and ready to continue publishing C
  72.       News.  
  73.  
  74.       Over  the last couple of weeks, numerous letters, postcards, and
  75.       electronic  mail  messages  have been received asking the status
  76.       of  C  News.   I  thank  all  of you for your support, and it is
  77.       because of this I plan to continue with C News.  
  78.  
  79.       [ Changes with C News ]
  80.  
  81.       There  have  been  two changes that I would like to discuss with
  82.       you  at this time.  One, C News will no longer attempt to adhere
  83.       to   a   strict   publication  schedule.   Due  to  severe  time
  84.       constraints  I  will be unable to support C News (and the C BBS)
  85.       as  much  as I have in the past.  However, in an attempt to help
  86.       out  with  the  creation and publication of C News each month or
  87.       so.   Two  long  time C News Readers and C BBS Users have joined
  88.       the  editorial staff: Dan Kozak and Jim Singleton.  Both Dan and
  89.       Jim  have  been  readers  of C News and users of the C BBS since
  90.       the  beginning,  and  have  volunteered to help with the monthly
  91.       publication  of  the  newsletter.  I welcome both Dan and Jim to
  92.       the group and hope that you will as well.  
  93.  
  94.       [ What's in this issue...]
  95.  
  96.       This  issue  of  C News features an article by Anthony Lander on
  97.       Linked  Lists,  continuation  of  Wayne  Dernoncourts Beginner's
  98.       column,  and  a product review by Jim Singleton.  I will refrain
  99.       from  mentioning  what will be in the next issue as the contents
  100.       have not been firmed up at this time.  
  101.  
  102.       [ Help!! ]
  103.  
  104.       The  editorial  staff  of  C News needs some assistance.  Wanted
  105.       are  articles  on  C  or  C++  programmimng  under MS-DOS, UNIX,
  106.       MINIX,   Amiga-DOS,   or  your  homebrew  op  system.   See  the
  107.       attachment  on  "How  to Contact C News" for more information on
  108.       how to contact us here at the C BBS.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.       Issue 15                  C News                               3
  120.  
  121.  
  122.       [ Postcards!!! ]
  123.  
  124.       Still  looking  for postcards from readers of C News.  Since the
  125.       last  issue  of  C  News,  numerous postcards have been received
  126.       from  around  the globe.  Still looking for some from the United
  127.       States, as the Rest of the Globe is currently in the lead.  
  128.  
  129.  
  130.            Well, until next month, C ya
  131.  
  132.  
  133.            Barry
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.       Issue 15                  C News                              4
  180.  
  181.  
  182.       ===============================================================
  183.       LINKED LISTS: AN INTRODUCTION BY ANTHONY LANDER
  184.       ===============================================================
  185.  
  186.       1  What is a Linked List?
  187.  
  188.       A  linked  list  is  a  structure,  which looks like a iron link
  189.       chain,  inside your computer memory, or on a disk.  Linked lists
  190.       are  good  ways  to  store data when the data has to remain in a
  191.       certain  order,  but has be changed frequently.  You can insert,
  192.       delete,  and  move  your data around very quickly without having
  193.       to shuffle and resort your entire data set.  
  194.  
  195.       Let's  look  at the chain analogy a little closer.  Imagine that
  196.       each  link  in  the  chain  represents one element of data.  The
  197.       data  is  stored  this  way,  linked  together,  so that you can
  198.       follow  along  the  chain  very  quickly  and  easily.   With  a
  199.       computer  it's  very easy to cut links out of the chain, add new
  200.       links  in,  wherever  we feel like it, or move links to differnt
  201.       places in the chain.  
  202.  
  203.       There  are  two  varieties  of  linked  lists, singly linked and
  204.       doubly  linked.   A  singly  linked  list consists of structures
  205.       which  contain  elements  of  data,  and  a  pointer to the next
  206.       element  in  the  list.   A  doubly  linked  list  consists of a
  207.       pointer  to  the  next  element  and the previous element in the
  208.       list.  
  209.  
  210.       Our  library deals with doubly linked lists because you can move
  211.       both  ways  along  the  chain,  forward  and  backward.   Doulby
  212.       linked   lists  are  more  suited  to  most  applications,  and,
  213.       besides,  a  singly  linked  list  is  really just a subset of a
  214.       doubly linked one.  
  215.  
  216.       Graphically, a doubly linked list looks something like this: 
  217.  
  218.  
  219.           --------------         --------------         --------------
  220.       --> |            |-------> |            |-------> |            |
  221.           |  Data 1    |         |  Data 2    |         |  Data 3    |
  222.           |            |         |            |         |            |
  223.       ----|            | <-------|            | <-------|            |
  224.           --------------         --------------         --------------
  225.  
  226.       Though  I  didn't  illustrate  it,  a  very  important aspect of
  227.       linked  lists  is  that  'Data  1'  doesn't  necessarily have to
  228.       physically precede 'Data 2', it just has to point to it! 
  229.  
  230.       The  practical  upshot  of  this  is this:  if, for example, you
  231.  
  232.  
  233.  
  234.  
  235.  
  236.       Issue 15                  C News                               5
  237.  
  238.  
  239.       wanted to delete 'data 2', all you'd have to do is point 'Data 
  240.       1's ---> forward arrow to 'Data 3', and 'Data 3's <--- backward
  241.       arrow  to  'Data 1'.  By doing this, 'Data 2' is never accessed,
  242.       because  the  pointers  skip  right  over it.  You never have to
  243.       resort   your  list  because  nothing  ever  moves,  physically.
  244.       Graphically, removing 'Data 2' would look like this: 
  245.  
  246.  
  247.            --------------         . . . . . . . .        --------------
  248.        --> |            |------------------------------> |            |
  249.            |  Data 1    |         .  D a t a 2  .        |  Data 3    |
  250.            |            |         .             .        |            |
  251.        ----|            | <------------------------------|            |
  252.            --------------         . . . . . . . .        --------------
  253.  
  254.       Similarly,  you  can  insert data elements.  Let's say we wanted
  255.       to  put 'Data 2' between 'Data 6' and 'Data 7'.  This is what it
  256.       would look like when you put 'Data 2' back in place: 
  257.  
  258.  
  259.                                   --------------
  260.                            ,----> |            |-----,
  261.                            |      |  Data 2    |     |
  262.                            |      |            |     |
  263.                            |   ,--|            | <-, |
  264.                            |   |  --------------   | |
  265.            --------------  |   |                   | |    --------------
  266.        --> |            |--'   |                   | '--> |            |
  267.            |  Data 6    |      |                   |      |  Data 7    |
  268.            |            |      |                   |      |            |
  269.        ----|            | <----'                   '------|            |
  270.            --------------                                 --------------
  271.  
  272.  
  273.  
  274.       2  When would you use a linked list?
  275.  
  276.  
  277.       Linked  lists  are  a  good  choice whenever you have to insert,
  278.       delete,  move  move  elements around frequently, but always keep
  279.  
  280.  
  281.  
  282.  
  283.  
  284.       Issue 15                  C News                               6
  285.  
  286.  
  287.       them in a particular order.  
  288.  
  289.  
  290.            Examples of linked lists are:
  291.  
  292.  
  293.            -  The malloc() and free() functions that allocate and
  294.               deallocate blocks of contiguous memory in 'C.
  295.  
  296.  
  297.            -  Database applications (the pointers can point to file
  298.               records just as easily as locations in computer memory).
  299.  
  300.  
  301.            -  Text editors which have to insert, delete, and move lines
  302.               of text, but keep the file in order.
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.       Issue 15                  C News                               7
  342.  
  343.  
  344.  
  345.       3  The Library
  346.  
  347.  
  348.            The file LINE.ZOO contains the following files: 
  349.  
  350.       example.c      A small 'c program to demonstrate the use of the
  351.                      library.
  352.  
  353.       example.exe    An executable example file.
  354.  
  355.       line.c         The actual functions that compose the library.
  356.  
  357.       line.h         An #include file, with the structure for our
  358.                      linked list, and some #defines as well.
  359.  
  360.       fprot.h        Function prototypes for all the functions in the
  361.                      library.
  362.  
  363.       line.obj       line.c compiled with Microsoft C 5.1.
  364.  
  365.  
  366.       Though  the  library  is  written in Microsoft 'C 5.1, it should
  367.       port  to other compilers, and operating systems easily.  It only
  368.       calls  functions  the  standard  library  functions malloc() and
  369.       free(),  everything  else  is  done  through the manipulation of
  370.       pointers.  
  371.  
  372.       The  functions are written around a text processing application,
  373.       though  it  would  be  easy to change that to suit whatever data
  374.       you're  working  with.  The elements stored in our structure are
  375.       a line of text, and it's length.  
  376.  
  377.       3.1  The Structure
  378.  
  379.       Let's  start  off  by  examining  the  structure that holds each
  380.       element in the linked list.  
  381.  
  382.       struct line    {
  383.  
  384.            char *string;             /* Text of the line */
  385.  
  386.            int  length;              /* Length of the line */
  387.  
  388.  
  389.            struct line *prevline;  /* PTR to struct of prev line */
  390.  
  391.            struct line *nextline;  /* PTR to struct of next line */
  392.       };
  393.  
  394.  
  395.  
  396.  
  397.  
  398.       Issue 15                  C News                               8
  399.  
  400.  
  401.  
  402.       string  is a pointer to a line of text, and length is the length
  403.       of  the  text  line.   These are both data elements, and are not
  404.       really related to the mechanics of the linked list.  
  405.  
  406.       It  may  look weird, at first glance, to see a structure of type
  407.       line  defined  inside  a  structure  of  type  line.   Remember,
  408.       though,  prevline and nextline are only pointers, not structures
  409.       themselves.  
  410.  
  411.  
  412.                          --------------
  413.               . . .----> |            |-----> nextline
  414.                          |  Data X    |
  415.                          |            |
  416.            prevline <----|            | <----. . .
  417.                          --------------
  418.  
  419.       Examining  the  X'th  data element, we find that prevline points
  420.       to  previous  data element, which would be the X-1'th.  nextline
  421.       points to the next data element, in this case that's the 
  422.  
  423.            X+1'th.
  424.  
  425.       If  X  were  the FIRST element in the list, prevline would point
  426.       to  NULL,  or the -1th file record to indicate that we're at one
  427.       end  of the list.  Likewise if X were the LAST element, nextline
  428.       would  point to NULL or the -1th file element to show that we're
  429.       at the other end of the list.  
  430.  
  431.  
  432.       3.2  The Functions
  433.  
  434.       The  functions  in  the  library  can  be  broken  down  into  4
  435.       categories.  Let's take a look at them one by one.  
  436.  
  437.       This  first group of functions do not manipulate the position of
  438.       the lines in any way.  
  439.  
  440.       getnewline
  441.  
  442.       Call  this  function  whenever  you  want to put data into a new
  443.       structure.   This  function  returns a pointer to a struct line,
  444.       which  has  been  given space through malloc().  If there wasn't
  445.       enough space to create a line, then the function returns NULL.  
  446.  
  447.       This  doesn't  actually insert the line into the linked list, it
  448.  
  449.  
  450.  
  451.  
  452.  
  453.       Issue 15                  C News                               9
  454.  
  455.  
  456.       just  gets  some  space for it, and makes sure that the prevline
  457.       and  nextline  pointers  are  set to NULL.  Inserting lines into
  458.       the text comes later.  
  459.  
  460.  
  461.       [getfirstline, and getlastline]
  462.  
  463.       getfirstline()   returns  the  first  link  in  the  chain,  and
  464.       getlastline()  returns the last link in the chain.  If there are
  465.       no  elements  in  the chain, both of these functions will return
  466.       NULL.  
  467.  
  468.       This  next  group  of functions are used to insert elements into
  469.       the  list.   It's easier to think of it as adding links into the
  470.       middle of a chain.  
  471.  
  472.  
  473.       [insertafterline]
  474.  
  475.       This   function   will   take   a   struct   line,  returned  by
  476.       getnewline(),  and  place  it  after another, existing line.  To
  477.       add this lines to the end of the list, all you have to do is 
  478.  
  479.            .
  480.            .
  481.            .
  482.            struct line *last, *linetoinsert;
  483.            last = getlastline();
  484.            insertafterline(linetoinsert, last);
  485.            .
  486.            .
  487.            .
  488.  
  489.       Which  will  put  linetoinsert  after the last line in the list.
  490.       If  there  are  no  links  existing yet, then getlastline() will
  491.       return  NULL, and insertafterline() will understand this to mean
  492.       "put it at the end." 
  493.  
  494.       insertafterline()  isn't  restricted  to  adding to the end, you
  495.       could  insert  after,  say,  the  fifth  element, and make a new
  496.       sixth   element.    This   would  have  the  effect  of  pushing
  497.       everything  after the sixth up one, but of course, they wouldn't
  498.  
  499.  
  500.  
  501.  
  502.  
  503.       Issue 15                  C News                              10
  504.  
  505.  
  506.       actually have to move at all.  
  507.  
  508.       [insertbeforeline]
  509.  
  510.       insertbeforeline()    works    in   much   the   same   was   as
  511.       insertafterline()  does.  If you wanted to make a new first line
  512.       in  the  list,  you'd just have to call getfirstline(), and then
  513.       insertbeforeline() like the previous example.  
  514.  
  515.       The next group of functions delete lines from within the list.  
  516.  
  517.       deleteline
  518.  
  519.       Calling  deleteline() and telling it which line you want deleted
  520.       (by  passing  the  pointer to it), will free the memory occupied
  521.       by  the  line,  and  if string was allocated, it'll free that as
  522.       well.   Then  it  will  patch  up  all the prevline and nextline
  523.       pointers that are related to the record you're deleting.  
  524.  
  525.       deletealllines
  526.  
  527.  
  528.       deletealllines()  will  go  through,  and  free  up  all  of the
  529.       structures   allocated   by   getnewline(),   and   reinitialize
  530.       everything so that you can start a new list.  
  531.  
  532.       The  last  group  of  functions  in  the library move lines from
  533.       place  to place in the list.  Mostly, this is done through calls
  534.       to  insertbeforeline,  insertafterline, and deleteline.  They do
  535.       some  housekeeping,  though,  to make sure that everything is in
  536.       order.  
  537.  
  538.       [movebeforeline]
  539.  
  540.       This  will take a line *that's already in the list*, and move it
  541.       to   another   location,   before  another  line.   Results  are
  542.       unpredictable  if  you  tell it to move a line that's not in the
  543.       list somewhere.  
  544.  
  545.       [moveafterline]
  546.  
  547.       This  does  the  same  thing as movebeforeline(), except that it
  548.       will  put  the  line  you  want  to move after another line, not
  549.       before  it.   Same restriction applies with respect to lines not
  550.       already in the list.  
  551.  
  552.       4  The Example Program
  553.  
  554.       The  example  program  gives  you  an  idea  of how to use these
  555.       functions  in real life.  It creates some lines of text, makes a
  556.       list  of  them,  moves  them,  deletes  them,  inserts them, and
  557.       prints them, so that you can see what the results are.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.       Issue 15                  C News                              11
  564.  
  565.  
  566.  
  567.  
  568.       5  Winding Down
  569.  
  570.       Well,  that's  about it for linked lists.  I wrote the library a
  571.       few  months  back  for  a terminate and stay resident (TSR) text
  572.       editor,  which  never got off the ground.  If anybody would like
  573.       to  do  an  article on writing TSRs in Microsoft 'C, I'd be very
  574.       thankful <grin>.  
  575.  
  576.       There   are  certainly  things  that  could  be  added  to  this
  577.       library.   For starters, it can only handle one linked list at a
  578.       time.   What  if  you wanted to open two files at the same time?
  579.       A  possible  way  to  deal  with that is to put everything in an
  580.       outer,  enveloping structure.  That way, you could specify which
  581.       list  you'd like to work with for all of the insert, delete, and
  582.       move commands.  
  583.  
  584.       I'd  be  glad  to do more of these articles, if people find them
  585.       educational.  
  586.  
  587.  
  588.            Anthony Lander
  589.  
  590.            -----
  591.  
  592.       5637 Eldridge Ave,                   Fidonet     1:163/115.4
  593.       Montreal, Quebec,                    Alternet    7:483/4
  594.       CANADA H4W 2C9                       Compuserve  74017,544
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.       Issue 15                  C News                              12
  624.  
  625.  
  626.       ================================================================
  627.       BEGINNERS CORNER: By Wayne Dernoncourt
  628.       ================================================================
  629.  
  630.  
  631.       This  article  is scheduled to discuss why the separate steps of
  632.       editing  the  code,  compiling,  linking and finally running are
  633.       needed  to   writing  a  program  in  C.   This discussion isn't
  634.       restricted to C, and can  be applied to other languages.  
  635.  
  636.       In  some  versions  of  some languages, there is no need to edit
  637.       the   program  separately.   All  editing  is  done  within  the
  638.       language  itself.    For  example,  one system that I've used (a
  639.       Hewlett-Packard  9830)  came  up  in BASIC, the operating system
  640.       (such  as  it  was)  revolved around BASIC.   The computer would
  641.       check  each  line for syntax errors as it was being  entered and
  642.       wouldn't  accept a line with syntax errors.  When you wanted  to
  643.       run  the  program,  you  would  simply issue the RUN command and
  644.       the   computer  started  running.   The  only  problem with this
  645.       approach  was  that   it  was  incredibly slow (it took about 45
  646.       seconds  to  do  a FOR-NEXT loop  with a thousand iterations and
  647.       nothing  in  it!!).   This  approach  is  called an interpreter.
  648.       That  is  the  machine  must examine each line of a  program and
  649.       decode  the  characters  on  the  line for instructions and then
  650.       execute  the  instructions.   This  approach  isn't  much  of  a
  651.       problem  if  a   program  is being developed and debugged.  This
  652.       approach  has  advantages   because  it  is relatively simple to
  653.       write  an  interpreter  and  easy to  debug in this environment.
  654.       Interpreters  on  systems  that are now common  are much faster,
  655.       but  they're  still  relatively slow when compared to the  other
  656.       techniques to be discussed.  
  657.  
  658.       One  way  to  speed  this up is to "tokenize" the program.  That
  659.       is,  take  each  line  of  the  program  and  convert  the BASIC
  660.       commands  to  a  sort   of  shorthand  for  the  computer.  This
  661.       eliminates  the  first  step  of  the  interpretive process used
  662.       above,  that  of  decoding  the  ASCII  character   strings into
  663.       commands  for  the BASIC interpreter.  This is the process  used
  664.       by  the  BASIC  interpreter  that  comes  with  most  PC's.  The
  665.       program   must  be  written  and  edited with the BASIC compiler
  666.       (it's  tokenized as  it's written).  You can't use your favorite
  667.       text editor or a programming  editor (they don't tokenize).  
  668.  
  669.       Once  you  have  used  an  editor  to create a program, either a
  670.       standalone  or  one  that's  integrated with a language, you may
  671.       have   to  compile  it.   I  say  may  because  some  integrated
  672.       languages  are   true  interpreters that need no compiling.  But
  673.       lets  talk about  those that need compiling.  Whenever a program
  674.       is  compiled  and  linked, it is almost guaranteed to run faster
  675.       than  one  written in  an interpretive language (if the compiler
  676.       is  a  production  quality   compiler).   This  is  because  the
  677.       computer  doesn't  have  to  have  an   interpreter  examine the
  678.  
  679.  
  680.  
  681.  
  682.  
  683.       Issue 15                  C News                              13
  684.  
  685.  
  686.       source or tokenized file for commands.  
  687.  
  688.       In  addition,  some  compilers  will examine the source code for
  689.       code  that  has  no function (the FOR-NEXT loop mentioned above,
  690.       that  didn't  do  anything)  and  eliminate  it  or code that is
  691.       unreachable.   That  is  code  that  has  an  unconditional GOTO
  692.       before   it   and   either  no  statement  label  before  or  an
  693.       unreferenced  statement  label.   Compilers  will  also  perform
  694.       other  types   of  code optimization.  The compiler will examine
  695.       the   file   that   contains   the  source  code  (for  example:
  696.       EXAMPLE.C)  and  produce a file called the  object file (in this
  697.       case   EXAMPLE.OBJ).    This  file  contains  machine   language
  698.       instructions   (actual   instructions   to  the  CPU)  and  also
  699.       contains   function  calls  to system routines to do things such
  700.       as  program   initialization,  I/O and program termination.  The
  701.       compiler  knows what  these function calls are, but doesn't know
  702.       the  address  of  where  they   'live'.   The  location  of  the
  703.       function  calls  can  vary from system to  system.  The function
  704.       of  the  linker  is  to  resolve  these  last  differences.  The
  705.       linker  reads the object file and produces a file that  is ready
  706.       to  run.   This  file  can  have  a number of different types of
  707.       extensions depending on the operating system.  
  708.  
  709.       This   column  was  also  supposed  to  discuss  some  debugging
  710.       techniques.   The  best  debugging technique that I've ever come
  711.       across  is not to make  any mistakes.  Since that isn't terribly
  712.       practical,  the  next best thing  is to keep your mistakes small
  713.       and  easily  manageable.   This  is done by  starting out with a
  714.       good,  solid  design  (see  the first article).  Let's  continue
  715.       with some practical advice: 
  716.  
  717.       The  first  thing is damage control.  If your program is written
  718.       so  that  each  of  your  function calls only does one thing and
  719.       then  return.    You'll  be  making  your life easier during the
  720.       debugging phase.  
  721.  
  722.       Test  each  function  fully  before  you  start using it in your
  723.       program.   Create  a  short  main  program  that  only calls the
  724.       function  that  you've   created  and pass it data and check the
  725.       results.   You  should  already  have   come  up  with  what the
  726.       results  should  be  before you started.  If the  result differs
  727.       from  what  you  expected, find out what is wrong (you may  have
  728.       made  an error in your calculations).  This is much easier to do
  729.       at   this  stage  of  the game rather than when you're trying to
  730.       get the program  working.  
  731.  
  732.       A  short  analogy  may  be  in order here.  If you're building a
  733.       sports  car  and  you're  currently assembling the engine, you'd
  734.       examine  the pieces  to make sure that they were good before you
  735.       installed  them.   Can  you   imagine  how  much time, labor and
  736.       money  you  could  save yourself if you  found a bent crankshaft
  737.       when  you're  installing  it  instead of once you've  poured oil
  738.  
  739.  
  740.  
  741.  
  742.  
  743.       Issue 15                  C News                              14
  744.  
  745.  
  746.       into  the  engine  and  have  it  come  out  in a puddle at your
  747.       feet.   The  important  thing  is catch the errors while they're
  748.       still small  and easy to fix.  
  749.  
  750.       The  last  tip  that  I have is to use a symbolic debugger.  The
  751.       VAX  and  Turbo-C v2.0 both have full screen symbolic debuggers,
  752.       while  the   Prime has a symbolic debugger.  A symbolic debugger
  753.       will  let  you 'step'  through a program stopping on each source
  754.       line  and  examine  variables.    Alternatively you can have the
  755.       program  run  until  a certain variable is  accessed or changed.
  756.       The  full  screen  aspect of the debugger (not  available on the
  757.       Prime)  is that the source code is always in view on the  screen
  758.       at  the  currently  executing  line and you can have some of the
  759.       user   selected  variables  displayed  on the screen.  The exact
  760.       method varies  from system to system.  
  761.  
  762.       Wayne 
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798.  
  799.  
  800.  
  801.  
  802.  
  803.       Issue 15                  C News                              15
  804.  
  805.  
  806.       ================================================================
  807.       BEGINNERS CORNER PT II: By Wayne Dernoncourt
  808.       ================================================================
  809.  
  810.  
  811.       If  this  is  March,  it's  time  for  the column about the MAKE
  812.       utility.   Before  I  do  that  let  me discuss something rather
  813.       basic  about  writing  a  program.  No one new to a language can
  814.       learn  to write a masterpiece on their first attempt.  My advice
  815.       is  to  write  some  simple  programs  to understand some of the
  816.       simple  mechanics  of  what is going on in your program.  If you
  817.       have  nothing else, use variations on the ones that I present to
  818.       you.  
  819.  
  820.       Now  on  with  the  regularly scheduled portion of our show, the
  821.       MAKE  utility.   For  this, we have to refer back to the January
  822.       article.   In  that  article  we discussed putting each function
  823.       into  it's  own file.  We wanted to do this to make it easier to
  824.       debug  a  program.   Each  function  can  be verified as working
  825.       correctly  by  itself.   This  left  us  with  a  rough  time of
  826.       compiling  the  program,  so  I  had  you  concatenate the files
  827.       together.   Let's  go back and separate them out again, and I'll
  828.       show you a neat way to compile and link them together.  
  829.  
  830.       The  first  thing  that  you  have  to  do (besides creating the
  831.       source  files  for  the  functions), is to create a simple ASCII
  832.       file  (suggest using EDLIN) and calling it MYPROJ.PRJ.  The only
  833.       thing  important about this is the file type, it should be .PRJ,
  834.       for  reasons  I'll  explain shortly.  Put the following lines in
  835.       the file (each on it's own line) 
  836.  
  837.       INIT
  838.       GETDATA
  839.       COMPUTE
  840.       PRINTRESULTS
  841.  
  842.       Notice  that  the  filetype of .C isn't on any of the files.  It
  843.       doesn't   have  to  be  because  it's  assumed  to  be  of  that
  844.       filetype.   The filenames can be lowercase, the operating system
  845.       will  change  them  into  uppercase before it does anything else
  846.       with  them  anyway.   The  order isn't important, the only thing
  847.       that  matters  is  that  all  of  the  files needed for the main
  848.       program are mentioned in the list and spelled correctly.  
  849.  
  850.       The  next  thing  you  need to do is to get into Turbo-C and the
  851.       main  routine.   Do you see across the top of the screen, on the
  852.       left  it  says File, besides that Edit, then Compile, Run, Proj,
  853.       Options  &  Debug.  Use the F-10 key to get to that top menu and
  854.       press  the  P key to highlight the Project item and you'll get a
  855.       pull-down  menu  that  says  *.PRJ.  This indicates that Turbo-C
  856.       will  list  all  of the files in the current directory with that
  857.       file  type,  again  press  return,  and  the pull-down menu will
  858.  
  859.  
  860.  
  861.  
  862.  
  863.       Issue 15                  C News                              16
  864.  
  865.  
  866.       disappear.  
  867.  
  868.       Next  time  you  compile,  the compiler will check to see if the
  869.       .OBJ  file  (if  it  exists)  has a date later than the C source
  870.       file.   If  the  object  file was created after the source file,
  871.       the  compiler  checks  the next file in the list.  If the object
  872.       file  was  created  before  the source file, that indicates that
  873.       the  source file has changed and therefore recompiles the source
  874.       to  a  new  object  file.  After all of the checking & compiling
  875.       are  done,  the  linker  is  called using the files that it just
  876.       checked & compiled to link to the main routine.  
  877.  
  878.       This  facility  makes  it  very  useful  to  create libraries of
  879.       commonly  used  routines  and use them.  The concept is to build
  880.       routines  that  can  be used like building blocks.  Each routine
  881.       must  be debugged before you can use it, but once it is, you can
  882.       use it without the worry of writing and debugging it again.  
  883.  
  884.       Another  useful  tool that is becoming popular towards this goal
  885.       is  the {{prototype}}.  A prototype isn't the real thing, but it
  886.       represents  the  real thing.  A prototype airplane, for example,
  887.       looks  like  the real airplane, allowing engineers and mechanics
  888.       to  check  for  interference  and  any problems that might occur
  889.       during   manufacture.    A   C   {{prototype}}   has  a  similar
  890.       functionality.   It doesn't actually do anything, it doesn't get
  891.       called,  etc.   What  it  is  used for, is to make sure that the
  892.       calling  routines  have the correct number and type of arguments
  893.       as  the program is being compiled.  The default variable type on
  894.       functions,  both  calling and return is the integer.  The linker
  895.       makes  no  checks  on  the number or types of arguments that are
  896.       being  passed  to  the  called function.  This process occurs in
  897.       what  is called the C pre-processor.  A more rigorous version of
  898.       this  goes  on in what is called the LINT utility, that is, lint
  899.       as in nit-picking.  
  900.  
  901.       The  following  example,  this  showing how prototypes are used,
  902.       will   print   a  standard  heading  on  a  program  output  for
  903.       identification.  
  904.  
  905.       This  file is called heading.c and contains the following source
  906.       lines: 
  907.  
  908.       #include "mylib.h"
  909.       #include <stdio.h>
  910.  
  911.       void heading (void)
  912.  
  913.       printf ("\n");
  914.       printf ("This is a standard heading\n\n");
  915.       return;
  916.  
  917.       The file mylib.h contains the actual prototype and has the
  918.  
  919.  
  920.  
  921.  
  922.  
  923.       Issue 15                  C News                              17
  924.  
  925.  
  926.       following single line in it:
  927.  
  928.       void heading(void);
  929.  
  930.  
  931.       No  doubt  all  of the sharp eyed readers out there noticed that
  932.       sometimes  the include statement uses quotes and sometimes angle
  933.       brackets  are  used.   The  quotes  are  used  for user supplied
  934.       functions  and  angle  brackets  are  used  for  system supplied
  935.       functions  (like  stdio.h).   In  the  actual  prototype itself,
  936.       there  are a couple of words which look familiar, but used in an
  937.       unusual manner.  
  938.  
  939.       The  data  type void means, in this case, that no data is passed
  940.       into  the  function  and  no data is passed out of the function.
  941.       That  is  the whole essence of the prototype, you're telling the
  942.       compiler  what each function has going into it and coming out of
  943.       it  as  far  as  the  type  of  data  goes.   The only thing the
  944.       function  does  (in  this  case) is modify the appearance of the
  945.       screen.  
  946.  
  947.       To  actually  use  this function in a program, try the following
  948.       test program and see what happens: 
  949.  
  950.       #include "mylib.h"
  951.  
  952.       main()
  953.  
  954.       {
  955.  
  956.       printf ("This is a test of the heading function\n");
  957.  
  958.       heading();
  959.  
  960.       }
  961.  
  962.       The  reason  that  the header file (mylib.h) is included in both
  963.       the  function  routine and the main program is to make sure that
  964.       both  of  them  match  whenever  they're recompiled.  If one (or
  965.       both)  changes  without  the  prototype  definition  changing to
  966.       match,  an  error  will  be generated.  This error checking goes
  967.       back  to finding errors as soon as possible after you make them,
  968.       not  waiting  until you get the wrong numbers coming out of your
  969.       program.  
  970.  
  971.       In  one  of  the  recent  issues,  I discussed using the #define
  972.       statement,  but  I  was  vague.  I'll attempt to remedy that now
  973.       by  providing an example of how #define can be used to make your
  974.       programs  clearer.   This  will  involve two programs, one using
  975.       the defines, the other not.  
  976.  
  977.       Both  of  the  following  programs  are  to  find  the  area and
  978.  
  979.  
  980.  
  981.  
  982.  
  983.       Issue 15                  C News                              18
  984.  
  985.  
  986.       perimeter  of  a  circle.   The first uses an #include statement
  987.       for  a  file called CIRCLE.H, the second has all of the relevant
  988.       arithmetic  hardcoded  into  the program.  The #define statement
  989.       takes  effect  during the pre-processor phase of compilation and
  990.       functions  as  a MACRO string substitution facility.  All of the
  991.       code is placed in line with the surrounding code.  
  992.  
  993.       The following three lines are in CIRCLE.H:
  994.  
  995.       #define  PI 3.14159
  996.       #define  PERIMOFCIRCLE(p,d) p=d*PI/2.0
  997.       #define AREAOFCIRCLE(a,d) a=d*d*PI/4.0
  998.  
  999.       The main code contains the following lines of code:
  1000.  
  1001.       #include <stdio.h>
  1002.       #include "circle.h"
  1003.       main()
  1004.       {
  1005.       float  diam, perim, area;
  1006.       diam = 2.;
  1007.       PERIMOFCIRCLE (perim, diam);
  1008.       AREAOFCIRCLE (area, diam);
  1009.       printf  ("Diameter: %f\nPerimeter: %f\nArea: %f\n", diam,
  1010.  
  1011.            perim, area);
  1012.       }
  1013.  
  1014.       or:
  1015.  
  1016.       #include <stdio.h>
  1017.       main()
  1018.       {
  1019.       float diam, perim, area;
  1020.       diam = 2.;
  1021.       perim = 1.570795*diam;
  1022.       area = 0.7853975*diam*diam;
  1023.       printf ("Diameter: %f\nPerimeter: %f\nArea: %f\n", diam,
  1024.  
  1025.            perim, area);
  1026.       }
  1027.  
  1028.       Of  course this is an extreme example.  The code isn't commented
  1029.       in  any  way  (this isn't a recommended coding practice), but it
  1030.       does  make  the  point that you can make your life easier if you
  1031.       put symbols (like PI) in for common things in your program.  
  1032.  
  1033.  
  1034.       Both  of the Prime and DECUS user groups have developed versions
  1035.       of  the  MAKE  utility  for their respective systems, but I have
  1036.       never  had  a  chance  to use them.  Prototypes are available on
  1037.       the  VAX,  but  aren't  available on the Prime using the current
  1038.       version of C that we have on the system.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.       Issue 15                  C News                              19
  1045.  
  1046.  
  1047.  
  1048.       C ya Next Month.....
  1049.  
  1050.       Wayne 
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061.  
  1062.  
  1063.  
  1064.  
  1065.  
  1066.  
  1067.  
  1068.  
  1069.  
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.       Issue 15                  C News                              20
  1106.  
  1107.  
  1108.       ================================================================
  1109.       ARTICLE SUBMISSION STANDARDS
  1110.       ================================================================
  1111.  
  1112.  
  1113.  
  1114.            All articles,  reviews  and  letters  to  editor  should be
  1115.       submitted  in  a ASCII formatted file.  Please use 0-65 margins,
  1116.       and  single  space.   Do not format the text in anyway, as I use
  1117.       Proff  to format C News.  Proff takes care of the justification,
  1118.       footnotes and headers.  
  1119.  
  1120.  
  1121.            You can  send  in  your article on a diskette to the C BBS,
  1122.       or  upload  it  to  the  C BBS. See "How to Contact us here at C
  1123.       News" for more information.  
  1124.  
  1125.       Barry
  1126.  
  1127.  
  1128.  
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134.  
  1135.  
  1136.  
  1137.  
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143.  
  1144.  
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.       Issue 15                  C News                              21
  1166.  
  1167.  
  1168.       ================================================================
  1169.       HOW TO GET HOLD OF US HERE AT CNEWS
  1170.       ================================================================
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.            The primary address for C News is as follows:
  1177.  
  1178.  
  1179.            C News
  1180.  
  1181.            % BCL Limited
  1182.  
  1183.            P.O. Box 9162
  1184.  
  1185.            McLean, VA 22102
  1186.  
  1187.            USA
  1188.  
  1189.  
  1190.            The primary electronic address for C News is the C BBS:
  1191.  
  1192.  
  1193.            C BBS
  1194.  
  1195.            1-703-644-6478
  1196.  
  1197.            2400,8,N,1 23hrs a day.
  1198.  
  1199.  
  1200.            1:109/307 in Fidonet.
  1201.  
  1202.  
  1203.            The secondary electronic address for C News is:
  1204.  
  1205.  
  1206.            MCI Mail: BCL Limited
  1207.  
  1208.  
  1209.  
  1210.            Barry
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.